package com.unionx.unionxuf.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Repository;

import com.unionx.unionxuf.common.DynamicConnect;
@Repository("PersonDao")
public class PersonDaoImpl implements PersonDao {

	private Statement stmt = null;
	private Connection conn = null;
	private Map<String, Object> map = new HashMap<String, Object>();
	
	public Object getPerson(HttpServletRequest request) {
		map.clear();
		try {
			conn = (Connection) DynamicConnect.getConnectionInfo(request);
			stmt = conn.createStatement();
			String sql = "select cPersonCode,cPersonName from person";
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				Map<String, Object> row = new HashMap<String, Object>();
				row.put("cPersonCode", rs.getString(1));
				row.put("cPersonName", rs.getString(2));
				list.add(row);
			}
			map.put("status", 0);
			map.put("message", "方法请求成功");
			map.put("data", list);
			return map;
		} catch (SQLException e) {

			map.put("status", 1);
			map.put("message", "方法请求失败");
			map.put("data", "500");
			return map;
	
			
		}
	}

	public Object getPersons(HttpServletRequest request) {
		// TODO Auto-generated method stub
		map.clear();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			conn = (Connection) DynamicConnect.getConnectionInfo(request);
			stmt = conn.createStatement();
			String sql = "select cDepCode,cPersonCode,cPersonName from person";
		
			
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				Map<String, Object> row = new HashMap<String, Object>();
				row.put("cDepCode", rs.getString(1));
				row.put("cPersonCode", rs.getString(2));
				row.put("cPersonName", rs.getString(3));
				list.add(row);
			}
			map.put("status", 0);
			map.put("message", "方法请求成功");
			map.put("data", list);
			return list;
		} catch (SQLException e) {

			map.put("status", 1);
			map.put("message", "方法请求失败");
			map.put("data", "500");
			return list;
	
			
		}
	}

}
